09-11 - Math operations

VERSE 1:
We bring our time, we bring our treasure,
we lay them down before Your throne.
You will make them something greater,
more than we could ever know. (chorus)

VERSE 2:
We bring our gifts, we bring our power
place them in Your sov’reign hand.
You will take what we have given,
You will use it for Your plan. (chorus)

CHORUS:
Glory be to God, the Maker
glory be to God, Creator
Take our time, use our treasure
turn them into something greater:
Glory be to God, the Maker.

VERSE 3:
Though our hearts are weak from failure,
broken dreams and failed attempts,
show us that in ev’ry season,
You will fill our emptiness. (chorus)
(God the Maker, The Porter’s Gate)

Quiz 1 (in class)

Operators

Operation Operator Example Evaluates to
Addition + 2 + 2 4
Subtraction - 4 - 1 3
Multiplication * 1.5 * 2 3.0
Division / 5 / 2 2.5
Floor Division // 5 // 2 2
Modulus (remainder of division) % 5 % 2 1
Exponent ** 3**3 27

Resulting types

  • If any of the operands is a float, result will be a float. Otherwise (both are integer), result is an integer.

  • However, there is an exception: result of a division (not floor division) is always a float. Careful with that! (why? we’ll see in a moment)

Operator precedence

  • Always be careful with expressions using more than one operator! For example:
a = 3
b = 6
c = a + b * 2
print(c)
  • This evaluates as \(a + (b \times 2) = 3 + (6 * 2) = 3 + 12 = 15\)

Python operator precedence order:

  1. Parentheses: ()
  2. Exponents: **
  3. Multiplication, divisions and modulus: * / // %
  4. Addition and subtraction: + -
  5. Comparisons: <= < >= > == != is (next week)
  6. Boolean not (next week)
  7. Boolean and (next week)
  8. Boolean or (next week)

Operators in strings

  • Python also permits using SOME operators with strings. In a metaphorical way…
String Operation Metaphor Operator Example Evaluates to
Concatenation Addition + "Hey" + " " + "apple" "Hey apple"
Repetition Multiplication * "na" * 4 "nananana"
  • Other operators are not supported. Multiplication of a string with another string is also not supported. Both wouldn’t make so much sense…

Evaluating operations in strings

It is also possible to evaluate an expression coded as a string. For example:

expression = "2 * (4 + 6) / 3 - 5"
result = eval(expression)
print(result)
1.666666666666667

Math module

For other mathematical operations, you can import Python’s math module. Two ways to do that:

  • Import the whole module and use as needed: need to use the module name followed by a dot (.) before using a function or variable from the module.
import math
r = 3
circumference = 2 * r * math.pi
  • Import only the functions and variables you will need. Then, no need to use the qualifier “math.”
from math import pi
r = 3
circumference = 2 * r * pi

Some functions in the math module (documentation here)

Method Explanation
math.cos(x) Returns cosine of x (rads)
math.sin(x) Returns sine of x (rads)
math.tan(x) Returns tangent of x (rads)
math.degrees(x) Converts x radians to degrees
math.radians(x) Converts x degrees to rads
math.sqrt(x) Returns square root of x
  • You can also try to import all of the modules resources by using
from math import *
  • However, this is not recommended due to “namespace pollution” (having lots of names referenced that are actually not used)

Overflow

  • Integers and floats are representations in the memory of your computer. Therefore, there are value limits to these numbers.
    • If we pass the limit, we arrive at an overflow, and the number is not computed correctly.
  • For integers, Python dynamically increases the use of memory as the number grows. Therefore, in theory there is a limit (computer memory will be full), but in practice this will almost never be the case.
    • For example, try 2147483647 ** 200
    • This, however, is a Python feature. Other languages may not handle that.
  • For floats, numbers are limited by the size of the mantissa and exponent.
    • Remember: for \(1.2345 \times 10^7\), \(1.2345\) is the mantissa, and \(7\) is the exponent
    • In Python, maximum is about 3 digits for the exponent and 17 digits for the mantissa
    • For example, try: 2.0**2000
      • OverflowError

More info at https://docs.python.org/3/tutorial/floatingpoint.html

So, be careful when mixing numeric types!

  • On June 4, 1996 the European Space Agency launched the first Ariane 5 rocket:
    • the result of a decade of development, $8 billion
    • Exploded 40 seconds after lift-off with a $500 million satellite payload on board…
  • Cause was a software error related to a number type conversion and an overflow

http://www-users.math.umn.edu/~arnold/disasters/ariane.html